﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mmht.BT;

public partial class admin_spgl_xgxx : System.Web.UI.Page
{
    GetMovie gm = new GetMovie();
    public int pagesize = 13;
    public int pages = 0;
    public int page = 1;
    public int prePage = 1;
    public int forPage = 1;
    public int dataTotal = 0;
    public string page_url;
    public bool bl=false;
    DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["admin_id"] == null)
        {
            Response.Redirect("admin_logout.aspx");
            return;
        }
        if (!IsPostBack)
        {
            if (Request["page"] != null)
            {
                string p = Request["page"];
                page = int.Parse(p);
            }
            else
            {
                page = 1;
            }
            ViewState.Add("page", page);
            page_url = Request.Url.ToString().Split('?')[0];
            controlRepeater();
        }
    }
    private void controlRepeater()
    {
        if (ViewState["page"] != null)
        {
            page = (int)ViewState["page"];
        }
        DataTable table1 = gm.Get_All_Movie();
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource  = table1.DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = pagesize;
        pages = pds.PageCount;
        dataTotal = table1.Rows.Count;
        page = page < 1 ? 1 : page;
        page = page > pages ? pages : page;
        forPage  = page + 1;
        prePage = page - 1;
        pds.CurrentPageIndex = page - 1;
        this.repeater1.DataSource = pds;
        this.repeater1.DataBind();
    }
    protected void rpt_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        int id = Convert.ToInt32(e.CommandArgument);
        //根据id获取pdf和缩略图的路径信息
        DataRow row = gm.GetmovieById(id);
        if (row == null)
        {
            this.lbMsg.Text = "删除对象不存在";
            return;
        }
        string movie_url = string.Empty;
        string image_url = string.Empty;
        if (row["movie_url"] != null)
        {
            movie_url = row["movie_url"].ToString();
            image_url = row["image_url"].ToString();
        }
        switch (e.CommandName)
        {
            case "del":
                FileDelete fd = new FileDelete();
                fd.deleteFile(movie_url);
                fd.deleteFile(image_url);
                bl = gm.DelMovie(id);
                if (bl)
                {
                    this.lbMsg.Text = "删除成功";
                    controlRepeater();
                }
                else
                {
                    this.lbMsg.Text = "操作失败";
                    controlRepeater();
                }
                break;
            case "update":
                Repeater repeater = (Repeater)sender;
                string name = ((TextBox)e.Item.FindControl("name")).Text.ToString();
                string author = ((TextBox)e.Item.FindControl("author")).Text.ToString();
                string remark = ((TextBox)e.Item.FindControl("remark")).Text.ToString();
                bl = false;
                dt = gm.GetMoviebyname(name);
                int count;
                if (dt == null)
                {
                    count = 0;
                }
                else 
                {
                    count = dt.Rows.Count;
                }
                
                if(name.Trim ().Length ==0)
                {
                    this.lbMsg.Text = "视频名称不可为空";
                    controlRepeater();
                    break;
                }
                if (count > 0)
                {
                    if (count == 1 && dt.Rows[0]["id"].ToString() == id.ToString ())
                    {
                        bl = gm.UpdateMovie(name, author, remark, id);
                    }
                    else
                    {
                        this.lbMsg.Text = "视频名称已存在至少一个，请改名";
                        controlRepeater();
                        break;
                    }
                }
                else
                {
                    bl = gm.UpdateMovie(name, author, remark, id);
                }
               
                if (bl)
                {
                    this.lbMsg.Text = "修改成功";
                    controlRepeater();
                }
                else 
                {
                    this.lbMsg.Text = "操作失败";
                }
                break;
            default :
                break;
        }

    }
    protected string checkout(bool bl) 
    {
        if (bl)
        {
            return "是";
        }
        else
        {
            return "否";
        }
    }
}
